package hg.framework.web.response;

import lombok.*;
import lombok.experimental.Accessors;
import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author entfrm
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功标记
     */
    private static int SUCCESS = 0;
    /**
     * 失败标记
     */
    private static int FAIL = -1;

    /**
     * 响应码
     */
    private int code;
    /**
     * 描述
     */
    private String msg;

    /**
     * 业务数据
     */
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, "请求成功");
    }

    public static <T> R<T> ok(String code, String... params) {
        return restResult(null, SUCCESS, params.toString());
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "请求成功");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, FAIL, "请求失败");
    }

    public static <T> R<T> failed(String code, String... params) {
        return restResult(null, FAIL, params.toString());
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, FAIL, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
